from sqlalchemy import Column, String, Float, BigInteger, DateTime, ForeignKey, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime
import uuid

Base = declarative_base()


class User(Base):
    __tablename__ = "users"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    username = Column(String(50), nullable=False)
    balance = Column(Float, nullable=False, default=10000.0)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # Relationships
    contracts = relationship("Contract", back_populates="user")
    balance_logs = relationship("BalanceLog", back_populates="user")


class Contract(Base):
    __tablename__ = "contracts"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
    symbol = Column(String(20), nullable=False)  # 'ETHUSDT', 'BTCUSDT'
    period = Column(String(10), nullable=False)  # '10m', '30m', '1h', '1d'
    direction = Column(String(10), nullable=False)  # 'up', 'down'
    bet_amount = Column(Float, nullable=False)
    anchor_price = Column(Float, nullable=False)
    anchor_timestamp = Column(BigInteger, nullable=False)  # milliseconds
    expiry_timestamp = Column(BigInteger, nullable=False)  # milliseconds
    final_price = Column(Float, nullable=True)
    profit = Column(Float, nullable=True)
    status = Column(String(20), nullable=False, default="active")  # 'active', 'settled', 'unknown'
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Relationships
    user = relationship("User", back_populates="contracts")
    balance_logs = relationship("BalanceLog", back_populates="contract")
    
    # Indexes
    __table_args__ = (
        Index('idx_contracts_status', 'status'),
        Index('idx_contracts_expiry', 'expiry_timestamp'),
        Index('idx_contracts_user', 'user_id'),
    )


class Price(Base):
    __tablename__ = "prices"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    symbol = Column(String(20), nullable=False)
    price = Column(Float, nullable=False)
    timestamp = Column(BigInteger, nullable=False)  # milliseconds
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Indexes
    __table_args__ = (
        Index('idx_prices_symbol_timestamp', 'symbol', 'timestamp'),
    )


class BalanceLog(Base):
    __tablename__ = "balance_logs"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
    contract_id = Column(String(36), ForeignKey("contracts.id"), nullable=True)
    amount = Column(Float, nullable=False)  # positive for credit, negative for debit
    balance_before = Column(Float, nullable=False)
    balance_after = Column(Float, nullable=False)
    type = Column(String(20), nullable=False)  # 'bet', 'profit', 'loss', 'refund'
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Relationships
    user = relationship("User", back_populates="balance_logs")
    contract = relationship("Contract", back_populates="balance_logs")
